package cn.lg.soar.system.biz.modules.general.service.impl;

import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.current.CurrentProxy;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.system.api.enums.TemplateTypeEnum;
import cn.lg.soar.system.biz.config.SystemCacheKey;
import cn.lg.soar.system.biz.modules.general.entity.Template;
import cn.lg.soar.system.biz.modules.general.mapper.TemplateMapper;
import cn.lg.soar.system.api.model.TemplateCache;
import cn.lg.soar.system.biz.modules.general.service.TemplateService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Collection;
import java.util.List;

/**
 * 模板
 * @author luguoxiang
 * @since 2025-03-03
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class TemplateServiceImpl extends BaseServiceImpl<TemplateMapper, Template> implements TemplateService, CurrentProxy<TemplateService> {

    @Cacheable(value = SystemCacheKey.TEMPLATE_BY_ID, key = "#id")
    @Override
    public TemplateCache getCacheById(Long id) {
        return baseMapper.getById(id);
    }

    @Cacheable(value = SystemCacheKey.TEMPLATE_BY_CODE, key = "#code")
    @Override
    public TemplateCache getCacheByCode(String code) {
        return baseMapper.getByCode(code);
    }

    @Caching(evict = {
            @CacheEvict(value = SystemCacheKey.TEMPLATE_BY_ID, key = "#entity.id"),
            @CacheEvict(value = SystemCacheKey.TEMPLATE_BY_CODE, key = "#entity.code"),
    })
    @Override
    public boolean updateById(Template entity) {
        return super.updateById(entity);
    }

    @Caching(evict = {
            @CacheEvict(value = SystemCacheKey.TEMPLATE_BY_ID, key = "#id"),
            @CacheEvict(value = SystemCacheKey.TEMPLATE_BY_CODE, allEntries = true),
    })
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Caching(evict = {
            @CacheEvict(value = SystemCacheKey.TEMPLATE_BY_ID, allEntries = true),
            @CacheEvict(value = SystemCacheKey.TEMPLATE_BY_CODE, allEntries = true),
    })
    @Override
    public boolean removeByIds(Collection<?> list) {
        return super.removeByIds(list);
    }

    @Override
    public String getContentAsText(String code) {
        TemplateCache templateCache = proxy().getCacheByCode(code);
        if (templateCache == null) {
            return null;
        }
        return templateCache.getContent();
    }

    @Override
    public InputStream getContentAsFile(String code) {
        TemplateCache templateCache = proxy().getCacheByCode(code);
        if (templateCache == null) {
            return null;
        }
        AssertUtil.isTrue(TemplateTypeEnum.file.eq(templateCache.getType()), "该模板不是文件类型");
        try {
            return new URL(templateCache.getContent()).openStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Template> listByGroup(String groupCode) {
        return baseMapper.listByGroup(groupCode);
    }

}
